package pl.engine.service
{
	import pl.engine.model.MapGrid;
	import pl.textures.Textures;
	import pl.textures.game.ISingleTexture;
	import pl.engine.model.IModel;
	import pl.engine.model.vo.BaseMapTile;
	import pl.engine.model.vo.obstacle.ObstacleType;
	import pl.engine.model.vo.obstacle.StaticObstacle;
	import pl.textures.TextureID;

	/**
	 * @author michalr
	 */
	public class MapTerrainProvider implements IMapTerrainProvider
	{
		public function provideTerrainTo( model:IModel, w:Number, h:Number ):void
		{
			var i:uint = 0;
			var n:uint = w * h;
			var j:uint = 0;
			var ox:uint = 0;
			var oy:uint = 0;
			var mapTile:BaseMapTile;
			var arr:Array = [];
			var px:uint;
			var py:uint;
			var obst:uint;
			var rand:uint;
			while (i < n)
			{
				if (arr[oy] == null) arr.push( [] );
				mapTile = BaseMapTile.createDefaultTile( ox, oy, MapGrid.TILE_W );
				j = 0;
				if (true)// (ox == 1 && oy == 1) || (ox == 1 && oy == 2)
				{
					while (j < 1)
					{
						px = Math.floor( mapTile.hID * mapTile.size + mapTile.size * .5 );
						py = Math.floor( mapTile.vID * mapTile.size + mapTile.size * .5 );
						rand = Math.round( Math.random() * 10 );
						obst = 0;
						if (rand >= 0 && rand <= 4)
						{
							obst = TextureID.BUSH;
						}
						else obst = TextureID.ROCK;
						obst = TextureID.TREE;
						mapTile.obstacles.push( StaticObstacle.factory( px, py, ObstacleType.STATIC, obst ) );
						// model.obstacle = StaticObstacle.factory( px, py, ObstacleType.STATIC, (ox == 3 && oy == 0) ? TextureID.MUSHROOM : TextureID.SIGN_POST);
						j++;
					}
					j = 0;
					while (j < 1)
					{
						if (ox == 3 && oy == 4)
						{
							px = Math.floor( mapTile.hID * mapTile.size + mapTile.size * .5 );
							py = Math.floor( mapTile.vID * mapTile.size + mapTile.size * .5 );
							mapTile.obstacles.push( StaticObstacle.factory( px, py, ObstacleType.STATIC, TextureID.TALL_TILE) );
						}
						else
						{
							px = Math.floor( mapTile.hID * mapTile.size + mapTile.size * Math.random() );
							py = Math.floor( mapTile.vID * mapTile.size + mapTile.size * Math.random() );
							mapTile.obstacles.push( StaticObstacle.factory( px, py, ObstacleType.STATIC, TextureID.TREE ) );
						}
						j++;
					}
				}
				(arr[oy] as Array).push( mapTile );
				if (ox == 0 || oy == 0)
				{
					mapTile.textures[0] = TextureID.BASE_END;
					mapTile.obstacles = [];
				}
				if (ox % (w - 1) == 0 && ox != 0)
				{
					mapTile.textures[0] = TextureID.BASE_END;
					mapTile.obstacles = [];
					ox = 0;
					oy++;
				}
				else
				{
					if (oy == h - 1)
					{
						mapTile.textures[0] = TextureID.BASE_END;
						mapTile.obstacles = [];
					}
					ox++;
				}
				i++;
			}

			w = h = Math.floor( w * .9 );
			var sx:uint = 1;
			var sy:uint = 1;
			i = 0;
			n = w * h;
			ox = sx;
			oy = sy;
			while (i < n)
			{
				mapTile = arr[ox][oy];
				mapTile.textures[0] = TextureID.GRASS;

				if (ox % (w - 1 + sx) == 0 && ox != 0)
				{
					ox = sx;
					oy++;
				}
				else
				{
					ox++;
				}
				i++;
			}
			trace( arr.length, arr[0]['length'] );

			model.setTerrain( arr );
		}
	}
}
